home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / filutil / bison110.zip / FILES.C < prev    next >
C/C++ Source or Header  |  1990-07-02  |  9KB  |  382 lines

  1. /* Open and close files for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #ifdef VMS
  22. #include <ssdef.h>
  23. #define unlink delete
  24. #ifndef XPFILE
  25. #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
  26. #endif
  27. #ifndef XPFILE1
  28. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  29. #endif
  30. #endif
  31.  
  32. #include <stdio.h>
  33. #include "system.h"
  34. #include "files.h"
  35. #include "new.h"
  36. #include "gram.h"
  37.  
  38. FILE *finput = NULL;
  39. FILE *foutput = NULL;
  40. FILE *fdefines = NULL;
  41. FILE *ftable = NULL;
  42. FILE *fattrs = NULL;
  43. FILE *fguard = NULL;
  44. FILE *faction = NULL;
  45. FILE *fparser = NULL;
  46.  
  47. /* File name specified with -o for the output file, or 0 if no -o.  */
  48. char *spec_outfile;
  49.  
  50. char *infile;
  51. char *outfile;
  52. char *defsfile;
  53. char *tabfile;
  54. char *attrsfile;
  55. char *guardfile;
  56. char *actfile;
  57. char *tmpattrsfile;
  58. char *tmptabfile;
  59.  
  60. extern char    *mktemp();    /* So the compiler won't complain */
  61. extern char    *getenv();
  62. FILE    *tryopen();    /* This might be a good idea */
  63. void done();
  64.  
  65. extern int verboseflag;
  66. extern int definesflag;
  67. int fixed_outfiles = 0;
  68.  
  69.  
  70. char*
  71. stringappend(string1, end1, string2)
  72. char *string1;
  73. int end1;
  74. char *string2;
  75. {
  76.   register char *ostring;
  77.   register char *cp, *cp1;
  78.   register int i;
  79.  
  80.   cp = string2;  i = 0;
  81.   while (*cp++) i++;
  82.  
  83.   ostring = NEW2(i+end1+1, char);
  84.  
  85.   cp = ostring;
  86.   cp1 = string1;
  87.   for (i = 0; i < end1; i++)
  88.     *cp++ = *cp1++;
  89.  
  90.   cp1 = string2;
  91.   while (*cp++ = *cp1++) ;
  92.  
  93.   return ostring;
  94. }
  95.  
  96.  
  97. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  98.    the output files, and opens the tmp files and the parser */
  99. void
  100. openfiles()
  101. {
  102.   char *name_base;
  103.   register char *cp;
  104.   char *filename;
  105.   int base_length;
  106.   int short_base_length;
  107.  
  108. #ifdef VMS
  109.   char *tmp_base = "sys$scratch:b_";
  110. #else
  111.   char *tmp_base = "/tmp/b.";
  112. #endif
  113.   int tmp_len;
  114.  
  115. #ifdef MSDOS
  116.   tmp_base = getenv ("TMP");
  117.   if (tmp_base == 0)
  118.     tmp_base = "";
  119.   strlwr (infile);
  120. #endif /* MSDOS */
  121.  
  122.   tmp_len = strlen (tmp_base);
  123.  
  124.   if (spec_outfile)
  125.     {
  126.       /* -o was specified.  The precise -o name will be used for ftable.
  127.      For other output files, remove the ".c" or ".tab.c" suffix.  */
  128.       name_base = spec_outfile;
  129. #ifdef MSDOS
  130.       strlwr (name_base);
  131. #endif /* MSDOS */
  132.       /* BASE_LENGTH includes ".tab" but not ".c".  */
  133.       base_length = strlen (name_base);
  134.       if (!strcmp (name_base + base_length - 2, ".c"))
  135.     base_length -= 2;
  136.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  137.       short_base_length = base_length;
  138.       if (!strncmp (name_base + short_base_length - 4, ".tab", 4))
  139.     short_base_length -= 4;
  140.       else if (!strncmp (name_base + short_base_length - 4, "_tab", 4))
  141.     short_base_length -= 4;
  142.     }
  143.   else if (spec_file_prefix)
  144.     {
  145.       /* -b was specified.  Construct names from it.  */
  146.       name_base = spec_name_prefix;
  147. #ifdef MSDOS
  148.       strlwr (name_base);
  149. #endif /* MSDOS */
  150.       /* Pretend this contains `.tab' whether it does or not.  */
  151.       base_length = strlen (name_base);
  152.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  153.       short_base_length = base_length;
  154.     }
  155.   else
  156.     {
  157.       /* -o was not specified; compute output file name from input
  158.      or use y.tab.c, etc., if -y was specified.  */
  159.  
  160.       name_base = fixed_outfiles ? "y.y" : infile;
  161.  
  162.       /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any.  */
  163.  
  164.       base_length = strlen (name_base);
  165.       if (!strcmp (name_base + base_length - 2, ".y"))
  166.     base_length -= 2;
  167.       short_base_length = base_length;
  168.  
  169. #ifdef VMS
  170.       name_base = stringappend(name_base, short_base_length, "_tab");
  171. #else
  172. #ifdef MSDOS
  173.       name_base = stringappend(name_base, short_base_length, "_tab");
  174. #else
  175.       name_base = stringappend(name_base, short_base_length, ".tab");
  176. #endif /* not MSDOS */
  177. #endif
  178.       base_length = short_base_length + 4;
  179.     }
  180.  
  181.   finput = tryopen(infile, "r");
  182.  
  183.   filename = getenv("BISON_SIMPLE");
  184. #ifdef MSDOS
  185.   /* file doesn't exist in curent directory, try in INIT directory */
  186.   cp = getenv("PATH");
  187.   if (filename == 0 && cp != 0)
  188.     {
  189.       filename = malloc(255);
  190.       _searchenv(PFILE, "PATH", filename);
  191.       if ( filename[0] == 0 )
  192.         strcpy(filename,PFILE);
  193.     }
  194. #endif /* MSDOS */
  195.   fparser = tryopen(filename ? filename : PFILE, "r");
  196.  
  197.   if (verboseflag)
  198.     {
  199. #ifdef MSDOS
  200.       outfile = stringappend(name_base, short_base_length, ".out");
  201. #else
  202.       if (spec_name_prefix)
  203.     outfile = stringappend(name_base, short_base_length, ".out");
  204.       else
  205.     outfile = stringappend(name_base, short_base_length, ".output");
  206. #endif
  207.       foutput = tryopen(outfile, "w");
  208.     }
  209.  
  210.   if (definesflag)
  211.     {
  212.       defsfile = stringappend(name_base, base_length, ".h");
  213.       fdefines = tryopen(defsfile, "w");
  214.     }
  215.  
  216. #ifdef MSDOS
  217.   actfile = mktemp(stringappend(tmp_base, tmp_len, "acXXXXXX"));
  218.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "atXXXXXX"));
  219.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "taXXXXXX"));
  220. #else
  221.   actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  222.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  223.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  224. #endif /* not MSDOS */
  225.  
  226.   faction = tryopen(actfile, "w+");
  227.   fattrs = tryopen(tmpattrsfile,"w+");
  228.   ftable = tryopen(tmptabfile, "w+");
  229.  
  230. #ifndef MSDOS
  231.   unlink(actfile);
  232.   unlink(tmpattrsfile);
  233.   unlink(tmptabfile);
  234. #endif
  235.  
  236.     /* These are opened by `done' or `open_extra_files', if at all */
  237.   if (spec_outfile)
  238.     tabfile = spec_outfile;
  239.   else
  240.     tabfile = stringappend(name_base, base_length, ".c");
  241.  
  242. #ifdef VMS
  243.   attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  244.   guardfile = stringappend(name_base, short_base_length, "_guard.c");
  245. #else
  246. #ifdef MSDOS
  247.   attrsfile = stringappend(name_base, short_base_length, ".sth");
  248.   guardfile = stringappend(name_base, short_base_length, ".guc");
  249. #else
  250.   attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  251.   guardfile = stringappend(name_base, short_base_length, ".guard.c");
  252. #endif /* not MSDOS */
  253. #endif /* not VMS */
  254. }
  255.  
  256.  
  257.  
  258. /* open the output files needed only for the semantic parser.
  259. This is done when %semantic_parser is seen in the declarations section.  */
  260.  
  261. void
  262. open_extra_files()
  263. {
  264.   FILE *ftmp;
  265.   int c;
  266.   char *filename, *cp;
  267.  
  268.   fclose(fparser);
  269.  
  270.   filename = (char *) getenv ("BISON_HAIRY");
  271. #ifdef MSDOS
  272.   /* file doesn't exist in curent directory, try in INIT directory */
  273.   cp = getenv("PATH");
  274.   if (filename == 0 && cp != 0)
  275.     {
  276.        filename = malloc(255);
  277.        _searchenv(PFILE1, "PATH", filename);
  278.        if ( filename[0] == 0 )
  279.          strcpy(filename,PFILE1);
  280.     }
  281. #endif
  282.   fparser= tryopen(filename ? filename : PFILE1, "r");
  283.  
  284.         /* JF change from inline attrs file to separate one */
  285.   ftmp = tryopen(attrsfile, "w");
  286.   rewind(fattrs);
  287.   while((c=getc(fattrs))!=EOF)    /* Thank god for buffering */
  288.     putc(c,ftmp);
  289.   fclose(fattrs);
  290.   fattrs=ftmp;
  291.  
  292.   fguard = tryopen(guardfile, "w");
  293.  
  294. }
  295.  
  296.     /* JF to make file opening easier.  This func tries to open file
  297.        NAME with mode MODE, and prints an error message if it fails. */
  298. FILE *
  299. tryopen(name, mode)
  300. char *name;
  301. char *mode;
  302. {
  303.   FILE    *ptr;
  304.  
  305. #ifdef MSDOS
  306.   char drive[3], path[128], fname[16], ext[16];
  307.  
  308.   _splitpath(name, drive, path, fname, ext);
  309.  
  310.   if ( strlen(fname) > 8 )
  311.     fname[8] = 0;
  312.   if ( strlen(ext) > 4 )
  313.     ext[4] = 0;
  314.  
  315.   _makepath(name, drive, path, fname, ext);
  316. #endif
  317.  
  318.   ptr = fopen(name, mode);
  319.   if (ptr == NULL)
  320.     {
  321.       fprintf(stderr, "bison: ");
  322.       perror(name);
  323.       done(2);
  324.     }
  325.   return ptr;
  326. }
  327.  
  328. void
  329. done(k)
  330. int k;
  331. {
  332.   if (faction)
  333.     fclose(faction);
  334.  
  335.   if (fattrs)
  336.     fclose(fattrs);
  337.  
  338.   if (fguard)
  339.     fclose(fguard);
  340.  
  341.   if (finput)
  342.     fclose(finput);
  343.  
  344.   if (fparser)
  345.     fclose(fparser);
  346.  
  347.   if (foutput)
  348.     fclose(foutput);
  349.  
  350.     /* JF write out the output file */
  351.   if (k == 0 && ftable)
  352.     {
  353.       FILE *ftmp;
  354.       register int c;
  355.  
  356.       ftmp=tryopen(tabfile, "w");
  357.       rewind(ftable);
  358.       while((c=getc(ftable)) != EOF)
  359.         putc(c,ftmp);
  360.       fclose(ftmp);
  361.       fclose(ftable);
  362.     }
  363.  
  364. #ifdef VMS
  365.   if (faction)
  366.     delete(actfile);
  367.   if (fattrs)
  368.     delete(tmpattrsfile);
  369.   if (ftable)
  370.     delete(tmptabfile);
  371.   if (k==0) sys$exit(SS$_NORMAL);
  372.   sys$exit(SS$_ABORT);
  373. #else
  374. #ifdef MSDOS
  375.   if (actfile) unlink(actfile);
  376.   if (tmpattrsfile) unlink(tmpattrsfile);
  377.   if (tmptabfile) unlink(tmptabfile);
  378. #endif /* MSDOS */
  379.   exit(k);
  380. #endif /* not VMS */
  381. }
  382.